//
//  UILabel+HWDUILabel.m
//  huangwdUI
//
//  Created by 张露杰 on 17/4/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "UILabel+HWDUILabel.h"

@implementation UILabel (HWDUILabel)

-(instancetype)initWithFrame:(CGRect)frame textAligment:(NSInteger)aligment font:(UIFont *)font textColor:(UIColor *)textColor text:(id)text numbelOfLines:(NSInteger)numbelOfLines
{
    if (self == [super initWithFrame:frame]) {
        self.textAlignment = aligment;
        self.font = font;
        self.textColor = textColor;
        self.numberOfLines = numbelOfLines;
        if ([text isKindOfClass:[NSString class]]) {
            self.text = text;
        }
        else if([text isKindOfClass:[NSAttributedString class]]){
            self.attributedText = text;
        }
    }
    return self;
}

-(void)setLineSpaceWithLineSpacing:(CGFloat)lineSpacing text:(NSString *)text
{
    if (lineSpacing < 0.01 || !text) {
        self.text = text;
        return;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, [text length])];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    [paragraphStyle setLineBreakMode:self.lineBreakMode];
    [paragraphStyle setAlignment:self.textAlignment];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    self.attributedText = attributedString;
}

- (void)setHighlightStirng:(NSString *)highLightString HighlightColor:(UIColor *)hightLightColor
{
    NSString *fullString;
    if (self.text && ![self.text isEqualToString:@""] && highLightString && ![highLightString isEqualToString:@""]) {
        fullString = [NSString stringWithFormat:@"%@", self.text];
    } else {
        return;
    }
    NSMutableAttributedString *attributedFullStr;
    if (self.attributedText) {
        attributedFullStr = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
    }
    else{
        attributedFullStr = [[NSMutableAttributedString alloc] initWithString:fullString];
    }
    NSRange range = [fullString rangeOfString:highLightString];
    NSDictionary *attributes = @{NSForegroundColorAttributeName : hightLightColor};
    [attributedFullStr addAttributes:attributes range:range];
    self.attributedText = attributedFullStr;
}

- (void)setTextFont:(NSString *)text font:(UIFont *)font
{
    NSString *fullString;
    if (self.text && ![self.text isEqualToString:@""] && text && ![text isEqualToString:@""]) {
        fullString = [NSString stringWithFormat:@"%@", self.text];
    } else {
        return;
    }
    NSMutableAttributedString *attributedFullStr;
    if (self.attributedText) {
        attributedFullStr = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
    }
    else{
        attributedFullStr = [[NSMutableAttributedString alloc] initWithString:fullString];
    }
    NSRange range = [fullString rangeOfString:text];
    NSDictionary *attributes = @{NSFontAttributeName:font};
    [attributedFullStr addAttributes:attributes range:range];
    self.attributedText = attributedFullStr;
}

@end
